package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Answer;
import cn.ztydata.teachingsystem.heaven.service.AnswerService;
import cn.ztydata.teachingsystem.heaven.web.dto.questionAnswer.AnswerDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.MessageFormat;

/**
 * 问题答案控制类
 *
 * @author lyj
 * @since 2015-3-20
 */
@Controller
@RequestMapping("/answer")
public class AnswerController extends BaseController {
    @Autowired
    private AnswerService answerService;
    @Autowired
    private WebContext webContext;

    /**
     * 新增问题答案
     *
     * @param answerDto 答案实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-20
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addAnswer(@Valid @RequestBody AnswerDto answerDto){
        Assert.notNull(answerDto.getAskId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));
        Assert.notNull(answerDto.getContent(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题内容"));

        answerService.addAnswer(mapper.map(answerDto, Answer.class));

        log.debug("新增问题答案");
        return new JsonResult(true, "新增成功！");
    }


    /**
     * 设置最佳答案
     *
     * @param answerDto
     * @return JsonResult
     * @author lyj
     * @since 2015-3-20
     */
    @RequestMapping(value = "/best", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult setBest(@Valid @RequestBody AnswerDto answerDto){
        Assert.notNull(answerDto.getId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "答案ID"));
        Assert.notNull(answerDto.getBest(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "最佳标识"));
        Assert.notNull(answerDto.getAskId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));

        answerService.setBest(mapper.map(answerDto, Answer.class));

        log.debug("设置最佳答案");
        return new JsonResult(true, "操作成功！");
    }


    /**
     * 删除答案
     *
     * @param id
     * @return JsonResult
     * @author lyj
     * @since 2015-3-20
     */
    @RequestMapping(value = "/delete/{id}/{askId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteAnswer(@PathVariable("id") long id,
                                   @PathVariable("askId") long questionId){
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "答案ID"));
        Assert.isTrue(questionId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));

        answerService.deleteAnswer(id, questionId);

        log.debug("删除答案");
        return new JsonResult(true, "删除成功！");
    }


    /**
     * 获取回答列表
     *
     * @param questionId 问题ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return
     * @author lyj
     * @since 2015-3-20
     */
    @RequestMapping(value = "/list/{questionId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getList(@PathVariable("questionId") long questionId,
                                  @RequestParam("page") int pageNum,
                                  @RequestParam("pageSize") int pageSize){
        Assert.isTrue(questionId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "答案ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        Page<Answer> page = answerService.getList(questionId, pageNum, pageSize);

        log.debug("获取回答列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), AnswerDto.class)));

    }

}
